package com.duoketech.controller.web.article;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.article.Article;
import com.duoketech.service.web.article.ArticleService;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class ArticleController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",10);
        Kv kv = Kv.by(Constant.MEMBER_ID_KEY,getShopMemberId());
        Page<Article> result = (Page<Article>) ArticleService.ME.pageAllRecord(getModel(Article.class),pageNum,pageSize,kv);
        for (Article each:result.getList()) {
            each.set(Article.STATUS,each.status());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            setAttr("article",ArticleService.ME.queryBy(id,getModel(Article.class),Kv.create()));
            render("edit-"+viewModel()+".html");
        } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            Article article = getModel(Article.class);
            article.set(Article.MEMBER_ID,getShopMemberId());
            ArticleService.ME.doAdd(article,Kv.create());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderSuccess(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            Article article = getModel(Article.class);
            article.set(Article.MEMBER_ID,getShopMemberId());
            ArticleService.ME.update(article,Kv.create());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderSuccess(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
            ArticleService.ME.deleteBy(id,getModel(Article.class),Kv.create());
            renderSuccess("操作成功");
        } catch (ValidateException e) {
            renderSuccess(e.getMessage());
        }
    }
    
    
    public void wxSync(){
    	render("wei-xin-sync.html");
    }
    
    
    public void doWxSync(){
    	String url = getPara("url","");
    	renderObject(ArticleService.ME.doWxSync(url));
    }
}
